package org.dev.app.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;

/**
 * @Description: MyBatisPlus实现代码自动生成
 * @Author: Wangzz
 * @Version: 1.0.0
 * @CreateDate: Created in 2021/10/17 16:47
 * @UpdateDate: [dateFormat:YYYY-MM-DD][modifier][description]
 */

// 代码自动生成器
public class MyBatisGlobalConfig {
    public static void main(String[] args) {
        // 需要构建一个代码自动生成器对象
        AutoGenerator mpg = new AutoGenerator();
        // 配置策略

        // 1. 全局配置D:\Enterprise_Projects\spring-cloud-demo\micro-service-cloud-provider-dept-8086\src\main\java
        GlobalConfig globalConfig = new GlobalConfig();
//        String projectPath = System.getProperty("user.dir");
        String projectPath = "D:\\Enterprise_Projects\\spring-cloud-demo\\micro-service-cloud-provider-dept-8086";
        globalConfig.setOutputDir(projectPath + "\\src\\main\\java");
        globalConfig.setAuthor("Wangzz");
        globalConfig.setOpen(false);
        globalConfig.setFileOverride(false); // 是否覆盖
        globalConfig.setServiceName("%sService"); // 去掉Serivice的I前缀
        globalConfig.setIdType(IdType.AUTO);
        globalConfig.setDateType(DateType.ONLY_DATE);
        globalConfig.setSwagger2(true);
        mpg.setGlobalConfig(globalConfig);

        // 数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl("jdbc:mysql://150.158.93.223:3306/demo_cloud?serverTimezone=Asia/Shanghai&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull");
        dataSourceConfig.setDriverName("com.mysql.jdbc.Driver");
        dataSourceConfig.setUsername("root");
        dataSourceConfig.setPassword("somepass");
        dataSourceConfig.setDbType(DbType.MYSQL);
        mpg.setDataSource(dataSourceConfig);

        // 包的配置
        PackageConfig packageConfig = new PackageConfig();
//        packageConfig.setModuleName("mpgdemo");
        packageConfig.setParent("org.dev.app");  // ★★★设置包路径
        packageConfig.setEntity("entity");
        packageConfig.setMapper("mapper");
        packageConfig.setService("service");
        packageConfig.setController("controller");
        mpg.setPackageInfo(packageConfig);

        // 策略的配置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setInclude("dept"); // ★ 设置要映射的表名
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
//        strategyConfig.setSuperEntityClass("你自己的父类实体，没有就不用设置！");
        strategyConfig.setEntityLombokModel(true); // 自动Lombok
        strategyConfig.setRestControllerStyle(true);
        strategyConfig.setLogicDeleteFieldName("deleted");
        // 自动填充配置
        TableFill gmtCreate = new TableFill("gmt_create", FieldFill.INSERT);
        TableFill gmtModified = new TableFill("gmt_modified", FieldFill.INSERT_UPDATE);
        ArrayList<TableFill> tableFills = new ArrayList<>();
        tableFills.add(gmtCreate);
        tableFills.add(gmtModified);
        strategyConfig.setTableFillList(tableFills);
        // 乐观锁配置
        strategyConfig.setVersionFieldName("version");
        strategyConfig.setRestControllerStyle(true);
        strategyConfig.setControllerMappingHyphenStyle(true);
        mpg.setStrategy(strategyConfig);

        mpg.execute(); // 执行


    }
}
